import {message} from 'antd';
import {getDevices, updateDevice, addDevice, deletDevice} from '../../services/store';

export default {
    namespace: 'deviceManage',
    state: {
        data: [],
        editorData: {},
        visible: false,
    },
    subscriptions: {
        setup(args) {
            const { dispatch, history } = args;
            history.listen(({ pathname }) => {
                if (pathname === '/store/device') {
                    dispatch({
                        type: 'init',
                    });
                }
            });
        }
    },
    effects: {
        *init(payload, { put }){
            yield put({
                type: 'load',
            });
        },
        *load(payload, { put, call}) {
            // const { storeId } = yield select((state => state.app.user));
            const { data, msg, code } = yield call(getDevices);
            console.log(data);
            if (code === 1) {
                yield put({
                    type: 'setState',
                    payload: {
                        data,
                    },
                });
            } else {
                console.log(msg);
            }
        },
        *editorOk({ payload: {err, isNew, values}}, {call, put}){
            if (err) {
                return;
            }
            let res;
            if (!isNew) {
                res = yield call(updateDevice, values);
            } else {
                // const {storeId} = yield select(state => state.app.user);
                // const body = {...values, storeId};
                console.log(values);
                res = yield call(addDevice, values);
            }
            const {code, msg } = res;
            if (code === 1) {
                message.success(msg);
                yield [
                    put({
                        type: 'editorVisible',
                    }),
                    put({
                        type: 'load',
                    }),
                ];
            } else {
                message.error(msg);
            }
        },
        * removeData({ selectId }, { call, put, }) {
            console.log('ii');
            const { code, msg, err } = yield call(deletDevice, {id: selectId});
            if (code === 1) {
                yield [
                    put({
                        type: 'init',
                    }),
                    call(message.success, msg)
                ];
            } else {
                message.error(msg, err);
            }
        },
    },
    reducers: {
        setState(state, {payload}) {
            return {
                ...state,
                ...payload,
            };
        },
        editorVisible(state, {payload: {editorData} = {}}){
            // 隐藏时不清数据,避免因为隐藏动作慢而界面显示为新建
            if (editorData) {
                return {
                    ...state,
                    visible: !!editorData,
                    editorData,
                };
            } else {
                return {
                    ...state,
                    visible: false,
                };
            }
        },
    }
};
